import { MountConfig } from "../../../resource/MountConfig";
import { ItemData } from "../../../resource/info/ItemData";
import { Language } from "../../../../../extend/language/Language";
import { ItemsUtil } from "../../shortcuts/item/ItemsUtil";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { ColorConfig } from "../../../resource/ColorConfig";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { EffectPlayer } from "../../../scene/player/item/EffectPlayer";
import { ModelType } from "../../../scene/modle/data/ModelType";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { ObjCreate } from "../../common/ObjCreate";
import { TilePanel } from "../../equipment/TilePanel";
import { MountStartsResultCellRender } from "./MountStartsResultCellRender";
import { ScrollPolicy } from "../../../../../fl/controls/ScrollPolicy";
import { MountStartsEvents } from "../events/MountStartsEvents";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { ResFileConst } from "../../../resource/ResFileConst";
import { ImagesConst } from "../../../resource/ImagesConst";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { PublicMessageConfig } from "../../../resource/PublicMessageConfig";
import { DataEvent } from "../../../events/DataEvent";
import { Cache } from "../../../cache/Cache";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { EProp } from "../../../../../Message/Public/EProp";
import { MsgManager } from "../../../manager/MsgManager";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { MountUtil } from "../../../scene/mount/MountUtil";
import { EColor } from "../../../../../Message/Public/EColor";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { GCheckBox } from "../../../../../com/mui/controls/GCheckBox";
import { TMountStars } from "../../../../../Message/Db/Tables/TMountStars";
import { _mountIdata } from "../../../../../this/_mountIdata";
import { _startsLv } from "../../../../../this/_startsLv";
type int = number;
//class MountStartsResultSp
    
    export  class MountStartsResultSp /* flash.display.Sprite */
    {
        constructor()
        {
            
            this.dpArr = [];
            this.initView();
            return;
        }

        public update(arg1: int): void
        {
            this._startsLv = arg1 + 1;
            if (!this._mountIdata || this._mountIdata.itemExInfo.m_haSt == "0") 
            {
                this.clean();
                return;
            }
            var loc1=[];
            if (this._mountIdata.itemExInfo.m_stLc[arg1]) 
            {
                loc1 = (this._mountIdata.itemExInfo.m_stLc[arg1] as string).split("#");
                this.updateMidShowBm(this._startsLv, loc1[1], loc1[0]);
            }
            else 
            {
                this.updateMidShowBm(1, 1, -1);
            }
            this._tData = MountConfig.instance.getMountStarts(this._startsLv);
            if (!this._tData) 
            {
                return;
            }
            this._propAmount = this._tData.propAmount;
            this._cost = this._tData.cost;
            this.updateData(arg1);
            this._materialIdata = new ItemData(this._tData.propCode);
            if (this._materialIdata) 
            {
                this._costTx.htmlText = "<font color=\'#B1efff\'>" + Language.getString(70185) + "</font>" + ItemsUtil.getItemName(this._materialIdata) + HTMLUtil.addColor(" * " + this._tData.propAmount, ColorConfig.instance.getItemColor(this._materialIdata.color).color);
                this._tongQianTx.text = this._tData.cost.toString();
            }
            return;
        }

        /* internal  */updateData(arg1: int): void
        {
            var loc4=0;
            this.dpArr = [];
            this._resultList.selectedIndex = -1;
            this._startsAmount = this._tData.starAmount;
            this.updateResultList(this._startsAmount);
            var loc1=this._mountIdata.itemExInfo.m_stLv[arg1];
            var loc2=[];
            var loc3=0;
            while (loc3 < this._startsAmount) 
            {
                if (loc1 && loc3 < loc1.length) 
                {
                    loc2 = (loc1[loc3] as string).split("#");
                    this.dpArr.push({"mountIData": _mountIdata, "lv": _startsLv, "AttValue": loc2[0], "color": loc2[1]});
                }
                else 
                {
                    this.dpArr.push({"mountIData": _mountIdata, "lv": _startsLv, "AttValue":-1, "color":-1});
                }
                ++loc3;
            }
            this.dpArr.sortOn("color", Array.DESCENDING | Array.NUMERIC);
            this._resultList.dataProvider = new DataProvider(this.dpArr);
            if (this._minSuccess) 
            {
                if (this._minAtt > 0) 
                {
                    loc4 = 0;
                    while (loc4 < this.dpArr.length) 
                    {
                        if (this._minAtt == this.dpArr[loc4].AttValue) 
                        {
                            this._resultList.selectedIndex = loc4;
                            break;
                        }
                        ++loc4;
                    }
                }
                else 
                {
                    this._resultList.selectedIndex = -1;
                }
            }
            else 
            {
                this._resultList.selectedIndex = -1;
            }
            this._resultList.drawNow();
            return;
        }

        /* internal  */updateResultList(arg1: int): void
        {
            var loc1=arg1 * 35;
            this._resultList.setSize(loc1, 34);
            this._resultList.columnCount = arg1;
            this._resultList.horizontalGap = 0;
            this._resultList.move((369 - loc1) / 2, 6);
            this._resultList.drawNow();
            return;
        }

        public playEffect(arg1: Boolean): void
        {
            this._minSuccess = arg1;
            if (this._effectPlayer) 
            {
                if (this._effectPlayer && this.contains(this._effectPlayer)) 
                {
                    this.removeChild(this._effectPlayer);
                }
                this._effectPlayer.stop();
            }
            else 
            {
                this._effectPlayer = new EffectPlayer();
                this._effectPlayer.playTotal = 1;
            }
            if (this.contains(this._effectPlayer) == false) 
            {
                this.addChild(this._effectPlayer);
            }
            this._effectPlayer.move(96, 45);
            if (arg1) 
            {
                this._effectPlayer.move(188, 95);
                this._effectPlayer.load("MountStartsCultureSuccess.swf", ModelType.NormalSwf, null);
            }
            else 
            {
                this._effectPlayer.move(188, 95);
                this._effectPlayer.load("MountStartsCultureFail.swf", ModelType.NormalSwf, null);
            }
            return;
        }

        /* internal  */stopEffect(): void
        {
            if (this._effectPlayer && this.contains(this._effectPlayer)) 
            {
                this._effectPlayer.stop();
                this.removeChild(this._effectPlayer);
            }
            return;
        }

        public get batchBtn(): GButton
        {
            return this._batchBtn;
        }

        /* internal  */initView(): void
        {
            this._bg = ObjCreate.createBitmap("", 1, 1, this);
            this._tileBg = ObjCreate.createBitmap("", 50, 2, this);
            this._midShowBm = ObjCreate.createBitmap("", 173, 79, this);
            this._resultList = new TilePanel();
            this._resultList.columnWidth = 35;
            this._resultList.rowHeight = 34;
            this._resultList.columnCount = 6;
            this._resultList.rowCount = 1;
            this._resultList.setStyle("skin", new Object /* flash.display.Bitmap */());
            this._resultList.setStyle("cellRenderer", MountStartsResultCellRender);
            this._resultList.setSize(288, 34);
            this._resultList.move(35, 9);
            this._resultList.horizontalScrollPolicy = ScrollPolicy.OFF;
            this._resultList.verticalScrollPolicy = ScrollPolicy.OFF;
            addChild(this._resultList);
            this._resultList.addEventListener(MountStartsEvents.ONRESULTCLICK, this.onresultClick);
            LoaderHelp.addResCallBack(ResFileConst.mountStarts, this.onLoadBmResource);
            this._singleBtn = ObjCreate.createGButton(Language.getString(70234), 99, 127, 66, 26, this, "ButtonSpecial");
            this._singleBtn.name = "singleBtn";
            this._batchBtn = ObjCreate.createGButton(Language.getString(70235), 214, 127, 66, 26, this, "ButtonSpecial");
            this._batchBtn.name = "batchBtn";
            this._singleBtn.toolTipData = Language.getString(70236);
            this._batchBtn.toolTipData = Language.getString(70265);
            this._singleBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClick);
            this._batchBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClick);
            this._valueTx = ObjCreate.createTextField("", 234, 84, 120, 20, this);
            ObjCreate.createBg(21, 158, 325, 3, this, ImagesConst.SplitLine);
            this._costTx = ObjCreate.createTextField("", 8, 163, 220, 20, this);
            this._useBindBox = ObjCreate.createCheckBox(Language.getString(70186), 230, 163, 124, 20, this);
            this._useBindBox.selected = true;
            this._tongQianTx = ObjCreate.createTextField("", 134, 163, 50, 20, this, GlobalStyle.textFormat2);
            this._tongQianTx.autoSize = flash.text.TextFormatAlign.RIGHT;
            ObjCreate.createBitmap(ImagesConst.Tongqian, 185, 163, this);
            var loc1=ObjCreate.createGTextField("", 298, 133, 66, 20, this, GlobalStyle.textFormat5);
            loc1.htmlText = Language.getString(71127);
            loc1.toolTipData = "<textFormat leading=\'4\'>" + PublicMessageConfig.instance.getMountModuleTextByName("MountStrarts") + "</textFormat>";
            return;
        }

        protected onresultClick(arg1: DataEvent): void
        {
            this.updateMidShowBm(arg1.data.lv, arg1.data.color, arg1.data.AttValue);
            return;
        }

        protected onBtnClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (arg1.target.name != "singleBtn") 
            {
                if (Cache.instance.pack.mountsPackCache.mountStarsCultureFlag == 0) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MountStartsGoCulture, {"lv": _startsLv, "posType": this._mountIdata.posType, "uid": this._mountIdata.uid, "autoBuy": Cache.instance.pack.mountsPackCache.statrsAutoBuy, "isSingle": false, "useBind": this._useBindBox.selected}));
                    return;
                }
            }
            else if (Cache.instance.pack.mountsPackCache.mountStarsCultureFlag == 0) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.MountStartsGoCulture, {"lv": _startsLv, "posType": this._mountIdata.posType, "uid": this._mountIdata.uid, "autoBuy": Cache.instance.pack.mountsPackCache.statrsAutoBuy, "isSingle": true, "useBind": this._useBindBox.selected}));
                return;
            }
            var loc1=Cache.instance.pack.backPackCache.getPropByType(EProp._EPropMountStarsCulture);
            var loc2=0;
            var loc3=0;
            while (loc3 < loc1.length) 
            {
                loc2 = loc2 + (loc1[loc3] as ItemData).itemAmount;
                ++loc3;
            }
            if (loc2 < this._propAmount && !Cache.instance.pack.mountsPackCache.statrsAutoBuy) 
            {
                MsgManager.showRollTipsMsg(Language.getString(71109));
                return;
            }
            var loc4;
            if ((loc4 = Cache.instance.role.money.coinBind + Cache.instance.role.money.coin) < this._cost) 
            {
                MsgManager.showRollTipsMsg(Language.getString(70060));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, this._cost - loc4));
                return;
            }
            var loc5=arg1.target;
            switch (loc5) 
            {
                case this._singleBtn:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MountStartsGoCulture, {"lv": _startsLv, "posType": this._mountIdata.posType, "uid": this._mountIdata.uid, "autoBuy": Cache.instance.pack.mountsPackCache.statrsAutoBuy, "isSingle": true, "useBind": this._useBindBox.selected}));
                    break;
                }
                case this._batchBtn:
                {
                    this.onMountStartsBantchWarnning(null);
                    break;
                }
            }
            return;
        }

        protected onMountStartsBantchWarnning(arg1: DataEvent=null): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MountStartsGoCulture, {"lv": _startsLv, "posType": this._mountIdata.posType, "uid": this._mountIdata.uid, "autoBuy": Cache.instance.pack.mountsPackCache.statrsAutoBuy, "isSingle": false, "useBind": this._useBindBox.selected}));
            return;
        }

        /* internal  */onLoadBmResource(): void
        {
            this._loaded = true;
            this._bg.bitmapData = GlobalClass.getBitmapData(ImagesConst.mountStartsResultBg);
            this._tileBg.bitmapData = GlobalClass.getBitmapData(ImagesConst.mountStartsTileBg);
            this.changeBm();
            return;
        }

        public updateInfo(arg1: ItemData, arg2: Boolean): void
        {
            this._mountIdata = arg1;
            if (this._mountIdata.itemExInfo.m_haSt == "0" || this._mountIdata.itemExInfo.m_haSt == "") 
            {
                this.clean();
            }
            else 
            {
                this._singleBtn.mouseEnabled = true;
                this._batchBtn.mouseEnabled = true;
                this._singleBtn.filters = [];
                this._batchBtn.filters = [];
            }
            return;
        }

        public updateMidShowBm(arg1: int, arg2: int, arg3: int=-1): void
        {
            var loc1=null;
            var loc2=null;
            this._minColor = arg2;
            this._minAtt = arg3;
            if (arg3 == -1) 
            {
                this._midShowBm.bitmapData = null;
                this._valueTx.htmlText = "";
                return;
            }
            if (this._loaded) 
            {
                this.changeBm();
            }
            if (arg2 > 0 && arg3 > 0) 
            {
                loc1 = MountUtil.instance.getColorStr(arg2);
                loc2 = "";
                loc2 = this._startsLv.toString() + "_" + arg2 + "_" + arg3;
                this._valueTx.htmlText = HTMLUtil.addColor(MountUtil.instance.getEjewelName(arg3) + " + " + MountConfig.instance.getLvColorAtt2Value(loc2), loc1);
            }
            else 
            {
                this._valueTx.htmlText = "";
            }
            return;
        }

        /* internal  */changeBm(): void
        {
            if (this._minColor != EColor._EColorWhite) 
            {
                if (this._minColor != EColor._EColorGreen) 
                {
                    if (this._minColor != EColor._EColorBlue) 
                    {
                        if (this._minColor != EColor._EColorPurple) 
                        {
                            if (this._minColor != EColor._EColorOrange) 
                            {
                                if (this._minColor != EColor._EColorRed) 
                                {
                                    this._midShowBm.bitmapData = null;
                                }
                                else 
                                {
                                    this._midShowBm.bitmapData = GlobalClass.getBitmapData(ImagesConst.mountStartsGoldStart);
                                }
                            }
                            else 
                            {
                                this._midShowBm.bitmapData = GlobalClass.getBitmapData(ImagesConst.mountStartsOrangeStart);
                            }
                        }
                        else 
                        {
                            this._midShowBm.bitmapData = GlobalClass.getBitmapData(ImagesConst.mountStartsPurpleStart);
                        }
                    }
                    else 
                    {
                        this._midShowBm.bitmapData = GlobalClass.getBitmapData(ImagesConst.mountStartsBuleStart);
                    }
                }
                else 
                {
                    this._midShowBm.bitmapData = GlobalClass.getBitmapData(ImagesConst.mountStartsGreenStart);
                }
            }
            else 
            {
                this._midShowBm.bitmapData = GlobalClass.getBitmapData(ImagesConst.mountStartsWhiteStart);
            }
            return;
        }

        public clean(): void
        {
            this.updateMidShowBm(1, 1, -1);
            this._singleBtn.mouseEnabled = false;
            this._batchBtn.mouseEnabled = false;
            this._singleBtn.filters = [FilterConst.colorFilter];
            this._batchBtn.filters = [FilterConst.colorFilter];
            return;
        }

        /* internal */ /* var */_bg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_tileBg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_singleBtn: GButton;

        /* internal */ /* var */_batchBtn: GButton;

        /* internal */ /* var */_resultList: TilePanel;

        /* internal */ /* var */_midShowBm: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_valueTx: Object /* flash.text.TextField */;

        /* internal */ /* var */_costTx: Object /* flash.text.TextField */;

        /* internal */ /* var */_tongQianTx: Object /* flash.text.TextField */;

        /* internal */ /* var */_useBindBox: GCheckBox;

        /* internal */ /* var */_startsLv: int;

        /* internal */ /* var */_mountIdata: ItemData;

        /* internal */ /* var */_loaded: Boolean;

        /* internal */ /* var */_isBatchTweening: Boolean;

        /* internal */ /* var */_startsAmount: int;

        /* internal */ /* var */dpArr: Array<any>;

        /* internal */ /* var */_materialIdata: ItemData;

        /* internal */ /* var */_tData: TMountStars;

        /* internal */ /* var */_propAmount: int;

        /* internal */ /* var */_minSuccess: Boolean;

        /* internal */ /* var */_effectPlayer: EffectPlayer;

        /* internal */ /* var */_minColor: int;

        /* internal */ /* var */_minAtt: int;

        /* internal */ /* var */_cost: int;
    }
